package com.baidu.yyb.prensenter;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.baidu.yyb.MMKV.NoteSp;
import com.baidu.yyb.actiivitys.MainActivity;
import com.baidu.yyb.actiivitys.NoteStartActivity;
import com.baidu.yyb.bean.UserData;
import com.baidu.yyb.constant.NoteTag;
import com.baidu.yyb.model.INoteModel;
import com.baidu.yyb.model.NoteModelImp;
import com.baidu.yyb.model.OnLoginCallback;
import com.baidu.yyb.model.OnRegisterListen;
import com.baidu.yyb.view.INoteView;

/**
 * @author LiuTong
 */
public class NotePresenter {
    private INoteModel iNoteModel;
    private INoteView iNoteView;
    private Handler handler = new Handler(Looper.getMainLooper());
    private boolean isLoginActivity = false;
    public NotePresenter(INoteView iNoteView) {
        this.iNoteView = iNoteView;
        iNoteModel = new NoteModelImp();
    }

    public void doLogin(){
        iNoteModel.login(iNoteView.getUserData(), new OnLoginCallback() {
            @Override
            public void success(UserData userData) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iNoteView.goActivity(MainActivity.class,null,true);
                    }
                });

            }

            @Override
            public void fail(String reason) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iNoteView.showWrongToast(reason);
                    }
                });

            }
        });
    }

    public void doRegister(){
        iNoteModel.register(iNoteView.getUserData(), new OnRegisterListen() {
            @Override
            public void onRegisterSuccess(UserData userData) {

                /****
                 * 存入数据库，更改为登录模式
                 */
                if(userData!=null){
                    iNoteModel.saveUSerDataWithSp(userData);
                    Bundle bundle = new Bundle();
                    bundle.putParcelable(NoteTag.USERDATA,userData);
                    bundle.putBundle(NoteTag.ACTIVITY_BUNDLE,bundle);
                    /***
                     * 注册成功跳转到登录页面
                     */
                    handler.post(() -> iNoteView.showLogin(bundle));

                }
            }

            @Override
            public void OnRegisterFail(String reason) {
                handler.post(() -> {
                    iNoteView.showWrongToast(reason);
                });

            }
        });
    }
    public void doLost(){

    }

    /***
     * 显示页面的类型
     */
    public void showDisplayMode(){
        if(isLoginActivity){
            iNoteView.showRegister();
        } else if(NoteSp.getBoolean(NoteTag.IS_FIRST_LAUNCH)){
            /***
             * 是第一次登录
             * 显示登录界面
             */
            iNoteView.showLogin(null);
        }else if(NoteSp.getBoolean(NoteTag.LOCK)){
            /****
             * 加锁
             * 或者弹出指纹锁
             */

            iNoteView.goActivity(null,null,true);
        }else {
            /***
             * 跳转到主页面
             */
            iNoteView.goActivity(MainActivity.class,null,true);
        }
        NoteSp.putBoolean(NoteTag.IS_FIRST_LAUNCH,false);


    }
}
